package com.chaihu.api.listener.admin.menugroup;

import com.alibaba.fastjson.JSONObject;
import com.chaihu.api.listener.AbstractServiceApiListener;
import com.chaihu.bean.in.admin.menugroup.DeleteMenuGroupIn;
import com.chaihu.bean.result.ResponseResult;
import com.chaihu.core.annotation.ChaihuListener;
import com.chaihu.core.context.DataFlowContext;
import com.chaihu.core.event.service.api.ServiceDataFlowEvent;
import com.chaihu.intf.admin.menugroup.DeleteMenuGroupService;
import com.chaihu.utils.constant.ServiceCodeMenuGroupConstant;
import com.chaihu.utils.util.Assert;
import com.chaihu.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

/**
 * 保存小区侦听
 */
@ChaihuListener("deleteMenuGroupListener")
public class DeleteMenuGroupListener extends AbstractServiceApiListener {

    @Autowired
    private DeleteMenuGroupService deleteMenuGroupService;

    @Override
    protected void validate(ServiceDataFlowEvent event, JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "gId", "组Id不能为空");
    }

    @Override
    protected void doSoService(ServiceDataFlowEvent event, DataFlowContext context, JSONObject reqJson) {
        DeleteMenuGroupIn menuGroupDto = BeanConvertUtil.covertBean(reqJson, DeleteMenuGroupIn.class);
        ResponseResult<Void> responseResult = deleteMenuGroupService.deleteMenuGroup(menuGroupDto);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(JSONObject.toJSONString(responseResult), HttpStatus.OK);
        context.setResponseEntity(responseEntity);
    }

    @Override
    public String getServiceCode() {
        return ServiceCodeMenuGroupConstant.DELETE_MENUGROUP;
    }

    @Override
    public HttpMethod getHttpMethod() {
        return HttpMethod.POST;
    }

    @Override
    public int getOrder() {
        return DEFAULT_ORDER;
    }

}
